package com.example.wonderfulradish;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

import com.example.TheParent.ParentManager;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    //声明动画类
    Animation popAnimation,smallRadish;
    ImageView popImageView,smallImageView;


    private Button parentmanager;
    private Button musicopen;
    private Button playstart;
    public  int flag=1;
    private MediaPlayer mp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //填充满整个屏幕
        if(Build.VERSION.SDK_INT>=21){
            View decorView=getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN|View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        //加载布局
        setContentView(R.layout.activity_main);

        //加载动画
        popAnimation=AnimationUtils.loadAnimation(this,R.anim.size_change);
        popImageView=(ImageView)this.findViewById(R.id.pop_image_view);
        popImageView.startAnimation(popAnimation);

        //加再黄色小萝卜动画
        smallRadish=AnimationUtils.loadAnimation(this,R.anim.position_anim);
        smallImageView=(ImageView)this.findViewById(R.id.smallradish);
        smallImageView.startAnimation(smallRadish);


        parentmanager=(Button) findViewById(R.id.parentmanager);
        musicopen=(Button) findViewById(R.id.musicopen);
        playstart=(Button) findViewById(R.id.playstart);
        playstart.setOnClickListener(this);
        musicopen.setOnClickListener(this);
        parentmanager.setOnClickListener(this);
        musicopen.setBackgroundResource(R.drawable.musicopen);

        madiaplayer();
    }
    private void madiaplayer(){
        mp= MediaPlayer.create(this, R.raw.sugar);
        mp.setLooping(true);
        try {
            //背景音乐准备
            mp.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //背景音乐播放
        mp.start();
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.parentmanager:
                AlertDialog.Builder builder=new AlertDialog.Builder(this);
                builder.setTitle("确认信息");
                builder.setMessage("如果你是家长，请确认本次操作由你本人完成");
                builder.setIcon(R.drawable.orange_luobo_big);
                builder.setCancelable(false);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent=new Intent(MainActivity.this,ParentManager.class);
                        startActivity(intent);
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        finish();
                    }
                });
                builder.setCancelable(true);
                builder.show();
                break;
            case R.id.playstart:
                Intent intent1=new Intent(MainActivity.this,Choose_Game.class);
                startActivity(intent1);
                break;
            case R.id.musicopen:
                if(flag==1){
                    musicopen.setBackgroundResource(R.drawable.musicoff);
                    mp.pause();
                    flag=0;
                } else{
                    musicopen.setBackgroundResource(R.drawable.musicopen);
                    mp.start();
                    flag=1;
                }

        }
    }
}
